#!/bin/bash

# 说明
# 用于一键安装docker以及docker-compose
# 镜像仓库地址：https://mirrors.aliyun.com/

echo '安装必要条件...'
sudo yum -y install {net-tools,wget,vim,unzip}

echo '卸载历史版本...'
sudo yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-selinux \
    docker-engine-selinux \
    docker-engine \
    docker-ce

echo '安装docker存储依赖包...'
sudo yum install yum-utils device-mapper-persistent-data lvm2 -y

echo '设置阿里云镜像源...'
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum makecache fast

echo '安装Docker中...'
sudo yum install docker-ce -y

echo '配置Docker镜像加速器...'
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
      "https://uy35zvn6.mirror.aliyuncs.com",
      "https://docker.hpcloud.cloud",
      "https://docker.m.daocloud.io",
      "https://docker.unsee.tech",
      "https://docker.1panel.live",
      "http://mirror.azure.cn",
      "https://dockerpull.org"
    ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
echo '配置完成！'

echo '启动中...'
sudo systemctl start docker
sudo systemctl enable docker
echo '启动完成！'

echo '安装docker-compose中...'
curl -L "https://mirrors.aliyun.com/docker-compose/releases/download/v2.32.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
curl -L "https://github.com/docker/compose/releases/download/v2.32.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

echo '安装完成!'
docker --version && docker-compose --version